<?php
/**
 * 消息管理
 * @author 479468212@qq.com
 */

namespace app\admin\controller;

use app\api\model\MessageLog as messageModel;

class MessageLog extends Base {

    /**
     * @OA\Get(
     *   path="/admin/MessageLog/index",
     *   tags={"消息相关"},
     *   summary="消息列表",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(name="type",in="query",description="检索类型",@OA\Schema( type="string" )),
     *   @OA\Parameter(name="keywords",in="query",description="关键词",@OA\Schema( type="string" )),
     *   @OA\Parameter(ref="#/components/parameters/size"),
     *   @OA\Parameter(ref="#/components/parameters/page"),
     *   @OA\Response(
     *     response="200",
     *     description="返回JSON", 
     *     @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *      )
     *    )
     *   ),
     *   @OA\Response(response="500", description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function index() {
        $limit   = $this->request->get('size', config('easyApi.ADMIN_LIST_DEFAULT'));
        $type    = $this->request->get('type');
        $keywords= $this->request->get('keywords');
        $obj = new messageModel();
        if($type == 'uid'){
            $obj = $obj->where(['uid'=>$keywords]);
        }
        if($type == 'content'){
            $obj = $obj->where('content','like','%'.$keywords);
        }
        $list    = $obj->where('status','<>',0)->order('id desc')->paginate($limit, false)->toArray();

        $this->success('获取成功',[
            'list'  => $list['data'],
            'count' => $list['total']
        ]);
    }

    /**
     * @OA\Post(
     *   path="/admin/MessageLog/add",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   tags={"消息相关"},
     *   summary="消息添加",
     *   @OA\RequestBody(
     *      @OA\MediaType(
     *         mediaType="application/json",
     *         @OA\Schema(
     *            @OA\Property(property="uid", type="string", description="用户"),
     *            @OA\Property(property="type", type="string", description="类型"),
     *            @OA\Property(property="content", type="string", description="内容")
     *         )
     *       )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK",
     *         @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *     ),
     *     @OA\Response(response="500", description="服务器错误"),
     *     security={{"App-Login-Token": ""}}
     * )
     */
    public function add() {
        $postData = $this->request->post();
        if(empty($postData['uid'])){
            $this->error("缺少用户");
        }
        if(empty($postData['content'])){
            $this->error("缺少内容");
        }
        if(empty($postData['type'])){
            $this->error("缺少类型");
        }
        $postData['send_uid'] = $this->userId;
        $postData['create_time'] = time();
        $res = messageModel::create($postData);
        if ($res === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * @OA\Delete(path="/admin/MessageLog/delete/{id}",
     *   tags={"客户端相关"},
     *   summary="删除版本号",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\Response(
     *     response=200,
     *     description="返回Json",
     *     @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *   ),
     *   @OA\Response(response=500, description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function delete($id) {
        if (!$id) {
            $this->error('缺少必要参数');
        }
        $result = messageModel::update([
            'id'   => $id,
            'status' => 0
        ]);
        if (!$result) {
            $this->error('删除失败');
        }
        $this->success();
    }

}
